package cn.web.web_module.updateweb;

import cn.itcast.pojo.orderItem;
import cn.itcast.pojo.user;
import cn.web.web_module.service.AdminRelevantService;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author: 张松
 * @Description:
 * @Date: Created in 9:51 2020/6/28
 * @Modified By:
 */

@RequestMapping("/exportHttp")
@RestController
public class MyExcelUtil {

    @Resource
    private AdminRelevantService adminRelevantService;



    @GetMapping("exportHttp")
    public String daoChu(HttpServletResponse response) {

        Date today = new Date(System.currentTimeMillis() - 1000 * 60 * 60 * 24);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String yesterday = simpleDateFormat.format(today);  //获取昨天日期

        Map<String, Object> yesterdayAdduserAndOrder = adminRelevantService.getYesterdayAdduserAndOrder();
        // 创建excel
        HSSFWorkbook wk = new HSSFWorkbook();
        // 创建一张工作表
        HSSFSheet sheet = wk.createSheet();
        // 控制列数字段,从累计用户数开始
        Short horsemenAddUserIndex = 2;

        List<user> horsemenAddUserlist = (List<user>) yesterdayAdduserAndOrder.get("horsemenAddUser"); //需要遍历的单元格和内容
        // 2
        sheet.setColumnWidth(0, 5000);
        HSSFRow row = sheet.createRow(0);
        HSSFRow row2 = sheet.createRow(1);
        // 创建第一行的第一个单元格
        // 想单元格写值
        HSSFCell cell = row.createCell((short) 0);
        row2.createCell(0).setCellValue(yesterday);
        cell.setCellValue("日期");

        if ((Integer)yesterdayAdduserAndOrder.get("AllAddUser")==null){
            yesterdayAdduserAndOrder.put("AllAddUser",0);
        }
        cell = row.createCell((short) 1);
        row2.createCell(1).setCellValue((Integer)yesterdayAdduserAndOrder.get("AllAddUser"));
        cell.setCellValue("新增用户数");

        if ((Integer)yesterdayAdduserAndOrder.get("allUserCount")==null){
            yesterdayAdduserAndOrder.put("allUserCount",0);
        }
        cell = row.createCell((short) 2);
        row2.createCell(2).setCellValue((Integer)yesterdayAdduserAndOrder.get("allUserCount"));
        cell.setCellValue("累计用户数");

        if (horsemenAddUserlist.isEmpty()){
            horsemenAddUserIndex++;
            cell = row.createCell((short) horsemenAddUserIndex);
            row2.createCell(horsemenAddUserIndex).setCellValue("昨日暂无推广用户");
            cell.setCellValue("昨日暂无推广用户");
        }else {
            Integer temp =0;
            for (user user : horsemenAddUserlist) {
                horsemenAddUserIndex++;
                cell = row.createCell((short) horsemenAddUserIndex);
                row2.createCell(horsemenAddUserIndex).setCellValue(user.getUserWxid());
                cell.setCellValue(user.getUserName() + "推广用户");
                temp+=user.getUserWxid();
            }
            cell = row.createCell((short) ++horsemenAddUserIndex);
            row2.createCell(horsemenAddUserIndex).setCellValue((Integer)yesterdayAdduserAndOrder.get("AllAddUser")-temp);
            cell.setCellValue( "其他渠道推广用户");
        }


        if (yesterdayAdduserAndOrder.get("countOrder")==null){
            yesterdayAdduserAndOrder.put("countOrder",0);
        }
        cell = row.createCell((short) ++horsemenAddUserIndex);
        row2.createCell(horsemenAddUserIndex).setCellValue((Integer)yesterdayAdduserAndOrder.get("countOrder"));
        cell.setCellValue("订单量");


        if (yesterdayAdduserAndOrder.get("completedOrder")==null){
            yesterdayAdduserAndOrder.put("completedOrder",0);
        }
        cell = row.createCell((short) ++horsemenAddUserIndex);
        row2.createCell(horsemenAddUserIndex).setCellValue((Integer)yesterdayAdduserAndOrder.get("completedOrder"));
        cell.setCellValue("已完成 ");


        if (yesterdayAdduserAndOrder.get("incompleteOrder")==null){
            yesterdayAdduserAndOrder.put("incompleteOrder",0);
        }
        cell = row.createCell((short) ++horsemenAddUserIndex);
        row2.createCell(horsemenAddUserIndex).setCellValue((Integer)yesterdayAdduserAndOrder.get("incompleteOrder"));
        cell.setCellValue("未完成");


        if (yesterdayAdduserAndOrder.get("excepOrder")==null){
            yesterdayAdduserAndOrder.put("excepOrder",0);
        }
        cell = row.createCell((short) ++horsemenAddUserIndex);
        row2.createCell(horsemenAddUserIndex).setCellValue((Integer)yesterdayAdduserAndOrder.get("excepOrder"));
        cell.setCellValue("异常订单 ");

        List<orderItem> yesterdayOrderWeightAndMoneyAndProductType = (List<orderItem>) yesterdayAdduserAndOrder.get("yesterdayOrderWeightAndMoneyAndProductType");
        if (yesterdayOrderWeightAndMoneyAndProductType==null){

        }else {
            for (orderItem orderItem : yesterdayOrderWeightAndMoneyAndProductType) {
                if (orderItem.getFinallyWeight()==null){
                    orderItem.setFinallyWeight(0.0);
                }
                cell=row.createCell((short)++horsemenAddUserIndex);
                row2.createCell(horsemenAddUserIndex).setCellValue(orderItem.getFinallyWeight()+"kg");
                cell.setCellValue(orderItem.getProductType()+"回收重量");

                if (orderItem.getSum()==null){
                    orderItem.setSum(0.0);
                }
                cell=row.createCell((short)++horsemenAddUserIndex);
                row2.createCell(horsemenAddUserIndex).setCellValue(orderItem.getSum()+"元");
                cell.setCellValue(orderItem.getProductType()+"回收金额");
            }
        }


        if (yesterdayAdduserAndOrder.get("allWeight")==null){
            yesterdayAdduserAndOrder.put("allWeight",0.0);
        }
        cell = row.createCell((short) ++horsemenAddUserIndex);
        row2.createCell(horsemenAddUserIndex).setCellValue((Double) yesterdayAdduserAndOrder.get("allWeight"));
        cell.setCellValue("回收重量");
        if (yesterdayAdduserAndOrder.get("allMoney")==null){
            yesterdayAdduserAndOrder.put("allMoney",0.0);
        }
        cell = row.createCell((short) ++horsemenAddUserIndex);
        row2.createCell(horsemenAddUserIndex).setCellValue((Double)yesterdayAdduserAndOrder.get("allMoney"));
        cell.setCellValue("回收金额 ");
        // 创建第一行
//        for (short i=0;i<list.size();i++)
//        {
//            row = sheet.createRow(i+1);
//            row.createCell(0).setCellValue(list.get(i).getWorkId());
//            row.createCell(1).setCellValue(list.get(i).getPositionName());
//            row.createCell(2).setCellValue(list.get(i).getAMendDate());
//            row.createCell(3).setCellValue(list.get(i).getAMstartDate());
//            row.createCell(4).setCellValue(list.get(i).getPMendDate());
//            row.createCell(5).setCellValue(list.get(i).getPMstartDate());
//        }
        try {
            /**
             * 弹出下载选择路径框
             */
            response.setContentType("application/octet-stream");
            response.setHeader("Content-disposition", "attachment;filename="+yesterday+"-UserAndOrder.xls");//默认Excel名称
            response.flushBuffer();
            wk.write(response.getOutputStream());//将数据用response 写出去
            wk.write(new FileOutputStream(new File("D://UserAndOrder.xls")));
            wk.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

        }
        return "null";
    }




    @GetMapping("EveryDayDataExcel")
    public void EveryDayDataExcel(HttpServletRequest request,HttpServletResponse response) {
        String sheetName ="每日数据表";
        String fileName = "每日数据表" + new Date()+".xls";
        HSSFWorkbook wb = new HSSFWorkbook();
        String [] title = {"日期","新增用户数","累积用户数","订单量","已完成","未完成","异常订单","回收总重量","回收总金额"};
        Map<String, Object> everyDayAddUserAndOrder = adminRelevantService.getEveryDayAddUserAndOrder();
        EveryDayDataExcel.getHssfWorkbook(sheetName,title,everyDayAddUserAndOrder,wb);

        try {
        this.setResponseHeader(response, fileName);
          OutputStream os = response.getOutputStream();
          wb.write(os);
          os.flush();
          os.close();
         } catch (Exception e) {
          e.printStackTrace();
         }
    }
    //发送响应流方法
    public void setResponseHeader(HttpServletResponse response, String fileName) {
        try {
            try {
                fileName = new String(fileName.getBytes(),"ISO8859-1");
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            response.setContentType("application/octet-stream;charset=ISO8859-1");
            response.setHeader("Content-Disposition", "attachment;filename="+ fileName);
            response.addHeader("Pargam", "no-cache");
            response.addHeader("Cache-Control", "no-cache");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

}
